package com.meet.controller;

import com.meet.dao.DepartmentDao;
import com.meet.dao.DepartmentDaoImpl;
import com.meet.dao.WorkersDao;
import com.meet.dao.WorkersDaoImpl;
import com.meet.entity.Department;
import com.meet.entity.Meeting;
import com.meet.entity.PageBean;
import com.meet.entity.Workers;
import com.meet.service.DepartmentService;
import com.meet.service.DepartmentServiceImpl;
import com.meet.service.WorkersService;
import com.meet.service.WorkersServiceImpl;
import com.meet.util.MD5Util;
import com.meet.vo.MeetingVo;
import com.meet.vo.WorkersVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/servlet/WorkersServlet")
public class WorkersServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        WorkersDao workersDao = new WorkersDaoImpl();
        WorkersService workersService = new WorkersServiceImpl(workersDao);
        DepartmentDao departmentDao = new DepartmentDaoImpl();
        DepartmentService departmentService = new DepartmentServiceImpl(departmentDao);
        //曾广龙写的
        HttpSession session = request.getSession();
        Workers worker = (Workers) session.getAttribute("worker");
//---------



        String flag = request.getParameter("flag");



        /**
         * flag=1&page=1 查所有 ；
         * flag=2增加
         * flag=3&id=1 跳修改页面；
         * flag=4 提交修改；
         * flag=5&id=1 删除
         */

        if("1".equals(flag)){
            WorkersVo workersVo = null;
            String currentPage = request.getParameter("page");//当前页码
            String rows = request.getParameter("rows");//每页显示条数

            if (currentPage == null || "".equals(currentPage)) {

                currentPage = "1";
            }

            if (rows == null || "".equals(rows)) {
                rows = "7";
            }
            PageBean<WorkersVo> pb = null;

            try {


                pb = workersService.getAllWorkersByPage(currentPage, rows);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            //3.将PageBean存入request
            request.setAttribute("pb", pb);

            //4.转发到main.jsp
            request.getRequestDispatcher("workersAll.jsp").forward(request, response);



        }else if("2".equals(flag)){

            String uName = request.getParameter("uName");
            String passeord =request.getParameter("password");
            String s = request.getParameter("sex");
            Boolean sex = true;
            if("男".equals(s)){
                sex = true;
            }else sex= false;
            String telephone = request.getParameter("telephone");
            String job = request.getParameter("job");
            passeord= MD5Util.encryptMD5(passeord); //加密
            Integer dId = Integer.valueOf(request.getParameter("cid"));
            Workers workers = new Workers(null,uName,passeord,sex,telephone,job,dId);

            System.out.println(workers);
            try {

                workersService.addWorkers(workers);
            } catch (SQLException e) {
                e.printStackTrace();
            }

            request.getRequestDispatcher("WorkersServlet?flag=1").forward(request,response);

        }else if("3".equals(flag)){

            Integer id = Integer.valueOf(request.getParameter("id"));
            String dName = request.getParameter("dName");
            List<Department> list = null;
            try {
                Workers workers = workersDao.getWorkers(id);
                request.setAttribute("workers", workers);
                list = departmentService.getAllDepartment(1,1000);
                request.setAttribute("dptNames",list);
                request.setAttribute("dName", dName);
                request.getRequestDispatcher("workersUpdate.jsp").forward(request, response);

            } catch (SQLException e) {
                e.printStackTrace();
            }

        }else if("4".equals(flag)){

            Integer id = Integer.parseInt(request.getParameter("id"));
            String uName = request.getParameter("uName");
            String passeord =request.getParameter("password");
            String s = request.getParameter("sex");
            Boolean sex = true;
            if("男".equals(s)){
                sex = true;
            }else sex= false;
            String telephone = request.getParameter("telephone");
            String job = request.getParameter("job");
            Integer dId = Integer.valueOf(request.getParameter("cid"));

            Workers workers = new Workers(id,uName,passeord,sex,telephone,job,dId);

            try {
                workersService.updateWorkers(workers);
            } catch (SQLException e) {
                e.printStackTrace();
            }

            request.getRequestDispatcher("WorkersServlet?flag=1").forward(request,response);

        }else if("5".equals(flag)){

            Integer id = Integer.valueOf(request.getParameter("id"));

            try {
               workersService.deleteWorkers(id);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            request.getRequestDispatcher("MeetingServlet?flag=1").forward(request, response);

        }else if("6".equals(flag)){

            //获取部门名称

            List<Department> list = null;

            try {
                list = departmentService.getAllDepartment(0,1000);
                request.setAttribute("dptNames",list);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

            request.getRequestDispatcher("workersAdd.jsp").forward(request,response);
        }else if("7".equals(flag)){
            Integer wid = worker.getId();
          //  Integer wid =3;
                    WorkersVo workersVo = null;
            String currentPage = request.getParameter("currentPage");//当前页码
            String rows = request.getParameter("rows");//每页显示条数

            if (currentPage == null || "".equals(currentPage)) {

                currentPage = "1";
            }

            if (rows == null || "".equals(rows)) {
                rows = "10";
            }
            PageBean<WorkersVo> pb = null;

            try {
//======曾广龙加
                workersVo = workersService.getWorkersVo(wid);
                request.setAttribute("workersVo",workersVo);
                //==

               // pb = workersService.getAllWorkersByPage(currentPage, rows);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            //3.将PageBean存入request
           // request.setAttribute("pb", pb);

            //4.转发到main.jsp
            request.getRequestDispatcher("myworkersAll.jsp").forward(request, response);

        }else if("8".equals(flag)){//员工个人自己修改自己的信息（手机电话）

                Integer id = Integer.parseInt(request.getParameter("id"));
                String telephone = request.getParameter("telephone");
                System.out.println(telephone);

                Workers workers = new Workers(id,null,null,null,telephone,null,null);

                try {
                    workersService.updateWorkersphone(workers);
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                request.getRequestDispatcher("WorkersServlet?flag=7").forward(request,response);

            }



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
